package model

import (
	"fmt"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/utils"

	"github.com/jinzhu/gorm"
)

type PatientInfo struct {
	*Model
	CheckId      string `json:"check_id" gorm:"Column:check_id"`
	PatientId    string `json:"patient_id" gorm:"Column:patient_id"`
	PatientName  string `json:"patient_name" gorm:"Column:patient_name"`
	Sex          uint8  `json:"sex" gorm:"Column:sex"`
	Age          uint16 `json:"age" gorm:"Column:age"`
	TimeStamp    string `json:"timeStamp" gorm:"Column:timeStamp"`
	UserId       uint32 `json:"userId" gorm:"Column:userId"`
	UserName     string `json:"userName" gorm:"Column:userName"`
	UserNickName string `json:"userNickName" gorm:"Column:userNickName"`
	UserType     uint32 `json:"userType" gorm:"Column:userType"`
	OrganType    int    `json:"-"`
}

type patientListInfo struct {
	List  []*PatientInfo
	Pager *app.Pager
}

type PatientListSwagger struct {
	*SwaggerSuccess
	Data *patientListInfo `json:"data"`
}

type PatientInfoSwagger struct {
	*SwaggerSuccess
	Data *PatientInfo `json:"data"`
}

func (p PatientInfo) TableName() string {
	return "t_patient_info"
}

// 查询检查列表
func (t PatientInfo) List(db *gorm.DB, option, beginTime, endTime string, pageOffset, pageSize int) ([]*PatientInfo, error) {
	var patients []*PatientInfo

	var query string
	query = "SELECT t2.id as id, t2.check_id, t2.patient_id, t2.patient_name, t2.sex, t2.age, t2.timeStamp, t1.id as userId, t1.userType, t1.userName, t1.userNickName"
	query += " FROM t_user t1"
	query += " LEFT JOIN t_patient_info t2 on t1.id=t2.userId"
	query += " LEFT JOIN t_organ_info t3 on t2.id=t3.patientId"
	query += " WHERE t2.isDelete=0 and t3.isDelete=0"
	//检查类型
	query += fmt.Sprintf(" AND t3.organType = %d", t.OrganType)
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND t2.timeStamp >= "%s" AND t2.timeStamp <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(t2.check_id, t2.patient_id, t2.patient_name, t1.userNickName) like "%%%s%%"`, option)
	}
	//排序
	query += " order by t2.timeStamp desc"
	//分页
	if pageOffset >= 0 && pageSize > 0 {
		query += fmt.Sprintf(" LIMIT %d, %d", pageOffset, pageSize)
	}

	res := db.Raw(query).Scan(&patients)
	if res.Error != nil {
		return nil, res.Error
	}

	return patients, nil
}

// 查询所有检查列表
func (t PatientInfo) ListWithoutType(db *gorm.DB, option, beginTime, endTime string, pageOffset, pageSize int) ([]*PatientInfo, error) {
	var patients []*PatientInfo

	var query string
	query = "SELECT distinct(t2.id) as id, t2.check_id, t2.patient_id, t2.patient_name, t2.sex, t2.age, t2.timeStamp, t1.id as userId, t1.userType, t1.userName, t1.userNickName"
	query += " FROM t_user t1"
	query += " LEFT JOIN t_patient_info t2 on t1.id=t2.userId"
	query += " LEFT JOIN t_organ_info t3 on t2.id=t3.patientId"
	query += " WHERE t2.isDelete=0 and t3.isDelete=0"
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND t2.timeStamp >= "%s" AND t2.timeStamp <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(t2.check_id, t2.patient_id, t2.patient_name, t1.userNickName) like "%%%s%%"`, option)
	}
	//排序
	query += " order by t2.timeStamp asc"
	//分页
	if pageOffset >= 0 && pageSize > 0 {
		query += fmt.Sprintf(" LIMIT %d, %d", pageOffset, pageSize)
	}

	res := db.Raw(query).Scan(&patients)
	if res.Error != nil {
		return nil, res.Error
	}

	return patients, nil
}

// 查询所有检查列表
func (t PatientInfo) ListWithoutTypeAndPager(db *gorm.DB, option, beginTime, endTime string) ([]*PatientInfo, error) {
	var patients []*PatientInfo

	var query string
	query = "SELECT distinct(t2.id) as id, t2.check_id, t2.patient_id, t2.patient_name, t2.sex, t2.age, t2.timeStamp, t1.id as userId, t1.userType, t1.userName, t1.userNickName"
	query += " FROM t_user t1"
	query += " LEFT JOIN t_patient_info t2 on t1.id=t2.userId"
	query += " LEFT JOIN t_organ_info t3 on t2.id=t3.patientId"
	query += " WHERE t2.isDelete=0 and t3.isDelete=0"
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND t2.timeStamp >= "%s" AND t2.timeStamp <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(t2.check_id, t2.patient_id, t2.patient_name, t1.userNickName) like "%%%s%%"`, option)
	}
	//排序
	query += " order by t2.timeStamp asc"

	res := db.Raw(query).Scan(&patients)
	if res.Error != nil {
		return nil, res.Error
	}

	return patients, nil
}

// 查询检查列表总数
func (t PatientInfo) Count(db *gorm.DB, option, beginTime, endTime string) (int, error) {
	var count int
	var query string
	query = "SELECT count(t2.id) as count FROM t_user t1"
	query += " LEFT JOIN t_patient_info t2 on t1.id=t2.userId"
	query += " LEFT JOIN t_organ_info t3 on t2.id=t3.patientId"
	query += " WHERE t2.isDelete=0 and t3.isDelete=0"
	//检查类型
	query += fmt.Sprintf(" AND t3.organType = %d", t.OrganType)
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND t2.timeStamp >= "%s" AND t2.timeStamp <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(t2.check_id, t2.patient_id, t2.patient_name, t1.userNickName) like "%%%s%%"`, option)
	}

	res := db.Raw(query).Count(&count)
	if res.Error != nil {
		return -1, res.Error
	}

	return count, nil
}

// 查询所有检查列表总数
func (t PatientInfo) CountWithoutType(db *gorm.DB, option, beginTime, endTime string) (int, error) {
	var count int
	var query string
	query = "SELECT count(distinct(t2.id)) as count FROM t_user t1"
	query += " LEFT JOIN t_patient_info t2 on t1.id=t2.userId"
	query += " LEFT JOIN t_organ_info t3 on t2.id=t3.patientId"
	query += " WHERE t2.isDelete=0 and t3.isDelete=0"
	//时间查询
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf(` AND t2.timeStamp >= "%s" AND t2.timeStamp <= "%s"`, beginTime, endTime)
	}
	//搜索条件
	if option != "" {
		query += fmt.Sprintf(` AND concat(t2.check_id, t2.patient_id, t2.patient_name, t1.userNickName) like "%%%s%%"`, option)
	}

	res := db.Raw(query).Count(&count)
	if res.Error != nil {
		return -1, res.Error
	}

	return count, nil
}

// 查询最新患者信息
func (t PatientInfo) LastPatient(db *gorm.DB) (*PatientInfo, error) {
	var patients []*PatientInfo
	query := fmt.Sprintf("select t1.id, t1.timeStamp from %s t1 inner join t_organ_info t2 on t1.id = t2.patientId where t1.isDelete = 0 and t2.isDelete=0 order by t1.id desc limit 1", t.TableName())
	res := db.Raw(query).Scan(&patients)
	//res := db.Table(t.TableName()).in.Select("id, timeStamp").Where("isDelete = 0").Last(&p)
	if res.Error != nil {
		return nil, res.Error
	}
	if len(patients) > 0 {
		return patients[0], nil
	} else {
		var patientInfo *PatientInfo
		patientInfo.TimeStamp = utils.NowTimeString()
		return patientInfo, nil
	}
}

// 根据文件ID获取患者信息
func (t PatientInfo) GetPatientInfoByFileId(db *gorm.DB, fileId int) (*PatientInfo, error) {
	var patients []*PatientInfo
	query := fmt.Sprintf("select t1.id, t1.timeStamp from %s t1 inner join t_video_file_info t2 on t1.fileId = t2.id where t1.isDelete = 0 and t2.isDelete = 0 and t2.id = %d", t.TableName(), fileId)
	res := db.Raw(query).Scan(&patients)
	//res := db.Table(t.TableName()).in.Select("id, timeStamp").Where("isDelete = 0").Last(&p)
	if res.Error != nil {
		return nil, res.Error
	}
	if len(patients) > 0 {
		return patients[0], nil
	} else {
		var patientInfo *PatientInfo
		patientInfo.TimeStamp = utils.NowTimeString()
		return patientInfo, nil
	}
}
